<script lang="ts" setup>
import type { GuessItem } from '@/types/home'
import { ref } from 'vue'
import { getGuessLikeAPI } from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import type { PageParams } from '@/types/grobal'

const finish = ref(false)

// 分页参数
const pageParams: Required<PageParams> = {
  page: 1,
  pageSize: 10,
}
// let cachedGuessList: GuessItem[]
// // 从本地存储加载数据（如果有的话）
// const loadCachedGuessList = () => {
//   if (pageParams.page === 1) {
//     const cached = uni.getStorageSync('firstPageGuessList')
//     if (cached) {
//       cachedGuessList = JSON.parse(cached)
//       guessList.value = [...cachedGuessList]
//     }
//   }
// }
// // 保存到本地存储
// const saveGuessListToCache = () => {
//   if (pageParams.page === 1 && guessList.value.length > 0) {
//     uni.setStorageSync('firstPageGuessList', JSON.stringify(guessList.value))
//   }
// }
// 猜你喜欢列表
const guessList = ref<GuessItem[]>([])
const getGuessList = async () => {
  if (finish.value === true) {
    return uni.showToast({ icon: 'none', title: '没有更多数据啦!' })
  }
  // 尝试从缓存加载数据
  // loadCachedGuessList()
  let res = await getGuessLikeAPI(pageParams)
  guessList.value.push(...res.result.items)
  // // 如果是第一页且缓存为空，则直接使用服务器返回的数据
  // if (pageParams.page === 1 && (!cachedGuessList || cachedGuessList.length === 0)) {
  //   guessList.value = [...res.result.items];
  // } else {
  //   // 否则，将新数据追加到现有数据之后（但注意，这里实际上我们只在非第一页时才追加）
  //   if (pageParams.page > 1) {
  //     guessList.value.push(...res.result.items);
  //   }
  // }
  // 更新页码
  if (pageParams.page < res.result.pages) {
    pageParams.page++
  } else {
    finish.value = true
  }
  // 只在第一页时保存缓存
  // saveGuessListToCache();
}
// 重置数据
const resetData = () => {
  pageParams.page = 1
  guessList.value = []
  finish.value = false
}
onLoad(() => {
  getGuessList()
})
// 暴露数据
defineExpose({
  resetData,
  getMore: getGuessList,
})
</script>
<template>
  <view class="caption">
    <text class="title"> 猜你喜欢 </text>
  </view>
  <view class="guess">
    <navigator
      class="guess-item"
      v-for="item in guessList"
      :key="item.id"
      :url="`/pages/goods/goods?id=${item.id}`"
    >
      <image class="image" mode="aspectFill" :src="item.picture"></image>
      <view class="name"> {{ item.name }} </view>
      <view class="price">
        <text class="small">¥</text>
        <text>{{ item.price }}</text>
      </view>
    </navigator>
  </view>
  <view class="loading-text">
    {{ finish ? '没有更多数据~' : '正在加载...' }}
  </view>
</template>

<style lang="scss">
:host {
  display: block;
}

/* 分类标题 */
.caption {
  display: flex;
  justify-content: center;
  line-height: 1;
  padding: 36rpx 0 40rpx;
  font-size: 32rpx;
  color: #262626;

  .text {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 28rpx 0 30rpx;

    &::before,
    &::after {
      content: '';
      width: 20rpx;
      height: 20rpx;
      background-image: url(@/static/images/bubble.png);
      background-size: contain;
      margin: 0 10rpx;
    }
  }
}

/* 猜你喜欢 */
.guess {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx;

  .guess-item {
    width: 345rpx;
    padding: 24rpx 20rpx 20rpx;
    margin-bottom: 20rpx;
    border-radius: 10rpx;
    overflow: hidden;
    background-color: #fff;
  }

  .image {
    width: 304rpx;
    height: 304rpx;
  }

  .name {
    height: 75rpx;
    margin: 10rpx 0;
    font-size: 26rpx;
    color: #262626;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .price {
    line-height: 1;
    padding-top: 4rpx;
    color: #cf4444;
    font-size: 26rpx;
  }

  .small {
    font-size: 80%;
  }
}

/* 加载提示文字 */
.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0;
}
</style>
